﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Data;
using System.ComponentModel;

namespace WF_Office_Verwaltung
{
    class helfer_klasse
    {
        dbCon_klasse db = new dbCon_klasse();
        nachrichten_klasse nk = new nachrichten_klasse();
        /// <summary>
        /// erstellen eines Logfiles
        /// </summary>
        /// <param name="message">Fehlermehldung </param>
        /// <param name="stacktrace">Stack Trace </param>
        internal void erstelleLogfile(string message, string stacktrace)
        {
            try
            {
                string date = DateTime.Now.ToString();
                string datum_ = date.Replace(" ", "_");
                string datum = datum_.Replace(":", "");
                string directory = @"c:\log\";
                string log_file = datum.Replace(".", "") + "_WF_OMT_log.txt";
                string path = directory + log_file;

                if (Directory.Exists(directory))
                {
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("Nachricht:\n");
                            sw.WriteLine(message);
                            sw.WriteLine("\n Stacktrace:\n");
                            sw.WriteLine(stacktrace);
                        }
                    }
                }
                else
                {
                    Directory.CreateDirectory(directory);
                    if (!File.Exists(path))
                    {
                        using (StreamWriter sw = File.CreateText(path))
                        {
                            sw.WriteLine("Nachricht:\n");
                            sw.WriteLine(message);
                            sw.WriteLine("\n Stacktrace:\n");
                            sw.WriteLine(stacktrace);
                        }
                    }

                }
            }
            catch (Exception ex)
            {
                nk.ErrorMessage(ex);
            }
        }
        /// <summary>
        /// Helfermethode die das Datum von String in Datetime umwandelt
        /// </summary>
        /// <param name="s_letzteWartung">Datum in String-Format</param>
        /// <returns></returns>
        internal DateTime convertStringToDate(string s_letzteWartung)
        {
            DateTime dt_letzteWartung = DateTime.Now;
            try
            {
                DateTimeConverter dtc = new DateTimeConverter();

                dt_letzteWartung = (DateTime)dtc.ConvertFromString(s_letzteWartung);
                return dt_letzteWartung;
            }
            catch (Exception ex)
            {
                nk.ErrorMessage(ex);
            }
            return dt_letzteWartung;
        }
        internal DateTime letzteWartung(int akt_kunde)
        {
            try
            {

                var dt_letzteWartung = db.get_letzten_Wartungsvertrag_List(akt_kunde);
                
                if (dt_letzteWartung != DateTime.Parse("01.01.1000"))
                {
                    return dt_letzteWartung;

                }
                return dt_letzteWartung;

            }
            catch (Exception ex)
            {
                nk.ErrorMessage(ex);
                throw;
            }

        }
    }
}
